<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title>WebSocket 聊天室</title></head>
<body>
<h2>聊天室</h2>
<textarea id="chat" rows="20" cols="50" readonly></textarea><br>
<input type="text" id="msg" placeholder="输入消息">
<button onclick="send()">发送</button>

<script>
    const socket = new WebSocket("ws://localhost:8081/ws");
    socket.onmessage = function(event) {
        document.getElementById("chat").value += event.data + "\n";
    };
    function send() {
        const msg = document.getElementById("msg").value;
        socket.send(msg);
        document.getElementById("msg").value = "";
    }
</script>
</body>
</html>
